package com.zhanghe.mall.web.service;

import com.zhanghe.mall.pojo.Account;
import com.zhanghe.mall.pojo.AccountPlatform;
import com.zhanghe.mall.pojo.AccountRole;
import com.zhanghe.mall.pojo.Manager;
import com.zhanghe.mall.pojo.repository.AccountPlatformRespository;
import com.zhanghe.mall.pojo.repository.AccountRespository;
import com.zhanghe.mall.pojo.repository.AccountRoleRepository;
import com.zhanghe.mall.service.ManagerService;
import com.zhanghe.mall.web.model.UserDetail;
import com.zhanghe.security.service.SimpleUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Throwable.class)
public class MgrUserDetailService implements SimpleUserDetailsService {

    @Autowired
    private ManagerService managerService;

    @Autowired
    private AccountRespository accountRespository;

    @Autowired
    private AccountPlatformRespository accountPlatformRespository;

    @Autowired
    private AccountRoleRepository accountRoleRepository;

    @Value("${zhanghe.system-id}")
    private Long systemId;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        Account account = accountRespository.findByUsername(userName);
        List<GrantedAuthority> authorities = new ArrayList<>();
        List<AccountRole> accountRoles = accountRoleRepository.findByAccount_IdAndRole_System_Id(account.getId(),systemId);
        for(AccountRole accountRole : accountRoles){
            GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(accountRole.getRole().getName());
            authorities.add(grantedAuthority);
        }
        return new UserDetail(account,authorities);
    }

    @Override
    public UserDetails loadUserByGithubId(Integer id) throws UsernameNotFoundException {
        AccountPlatform accountPlatform = accountPlatformRespository.findByPlatformId(String.valueOf(id));
        List<GrantedAuthority> authorities = new ArrayList<>();
        List<AccountRole> accountRoles = accountRoleRepository.findByAccount_IdAndRole_System_Id(accountPlatform.getAccount().getId(),systemId);
        for(AccountRole accountRole : accountRoles){
            GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(accountRole.getRole().getName());
            authorities.add(grantedAuthority);
        }
        if(accountPlatform!=null){
            return new UserDetail(accountPlatform.getAccount(),authorities);
        }else{
            return null;
        }
    }
}
